import Local_Window_EquipCommand from './windowEquipCommand.js'
let SWDA_EquipScene = {};

Scene_Equip.prototype.create = function() {
    Scene_MenuBase.prototype.create.call(this);
    this.createHelpWindow();
    this.createStatusWindow();
    this.createCommandWindow();
    this.createCompareWindow();
    this.createSlotWindow();
    this.createItemWindow();
    this.createActorPageWindow();    // 角色翻页
    this._lowerRightVisibility = true;
    this.updateLowerRightWindows();
    this.refreshActor();
};

// 创建窗口
Scene_Equip.prototype.createStatusWindow = function() {
    const [wx, wy, ww, wh] = [-24, -24, 600, 400];
    this._statusWindow = new Window_SkillStatus(wx, wy, ww, wh);
    this._statusWindow.reservePortraitImages();
    this.addChildAt(this._statusWindow._portraitSprite,2);    // 在底层显示立绘
    this.addWindow(this._statusWindow);
};

// 角色翻页
Scene_Equip.prototype.createActorPageWindow = function() {
    this._actorPageWindow = new Window_ActorPage();
    this._actorPageWindow.activate();
    this._actorPageWindow.setScene(this);
    this.addWindow(this._actorPageWindow);
};

// 详细说明菜单
SWDA_EquipScene.Scene_Equip_createHelpWindow = Scene_Equip.prototype.createHelpWindow;
Scene_Equip.prototype.createHelpWindow = function () {
    SWDA_EquipScene.Scene_Equip_createHelpWindow.call(this)
    this._helpWindow.x = 0;
    this._helpWindow.y = Graphics.boxHeight - this._helpWindow.height
}

// 【这个目前没用】
Scene_Equip.prototype.createCommandWindow = function() {
    const [wx, wy, ww] = [369, 40, 500];
    this._commandWindow = new Local_Window_EquipCommand(wx, wy, ww);
    this._commandWindow.setHelpWindow(this._helpWindow);
    //this._commandWindow.setHandler('equip', this.commandEquip.bind(this));
    //this._commandWindow.setHandler('optimize', this.commandOptimize.bind(this));
    //this._commandWindow.setHandler('clear', this.commandClear.bind(this));
    //this._commandWindow.setHandler('cancel', this.popScene.bind(this));
    this._commandWindow.setHandler('pagedown', this.nextActor.bind(this));
    this._commandWindow.setHandler('pageup', this.previousActor.bind(this));
    this.addWindow(this._commandWindow);
};

Scene_Equip.prototype.createSlotWindow = function() {
    //var wy = this._commandWindow.y + this._commandWindow.height;
    //var ww = Graphics.boxWidth / 2;
    //var wh = Graphics.boxHeight - wy;
    const [wx, wy, ww, wh] = [399, this._commandWindow.y + 65, 482, 480];
    this._slotWindow = new Window_EquipSlot(wx, wy, ww, wh);
    this._slotWindow.setHelpWindow(this._helpWindow);
    this._slotWindow.setHandler('ok',       this.onSlotOk.bind(this));
    this._slotWindow.setHandler('cancel',   this.popScene.bind(this));
    this.addWindow(this._slotWindow);
    this._slotWindow.setStatusWindow(this._compareWindow);
};


Scene_Equip.prototype.createItemWindow = function() {
    //var wy = this._slotWindow.y;
    //var ww = Graphics.boxWidth / 2;
    //var wh = Graphics.boxHeight - wy;
    const [wx, wy, ww, wh] = [399, this._commandWindow.y + 65, 482, 480];
    this._itemWindow = new Window_EquipItem(wx, wy, ww, wh);
    this._itemWindow._scrollbar.setOffset(5)
    this._itemWindow.setHelpWindow(this._helpWindow);
    this._itemWindow.setHandler('ok',     this.onItemOk.bind(this));
    this._itemWindow.setHandler('cancel', this.onItemCancel.bind(this));
    this._itemWindow.$on('select', this.updateItemPreviewWindow.bind(this));
    this._itemWindow.$on('deselect', this.hideItemPreviewBox.bind(this));
    this._slotWindow.setItemWindow(this._itemWindow);
    this.addWindow(this._itemWindow);
    this._itemWindow.hide();
    this._itemWindow.setStatusWindow(this._compareWindow);
};

Scene_Equip.prototype.createCompareWindow = function() {
    this._lowerRightWindows = [];
    //var wx = this._itemWindow.width;
    //var wy = this._itemWindow.y;
    //var ww = Graphics.boxWidth - wx;
    //var wh = Graphics.boxHeight - wy;
    const [wx, wy, ww, wh] = [881, this._commandWindow.y + 65, 368, 480];
    this._compareWindow = new Window_StatCompare(wx, wy, ww, wh);
    //this._slotWindow.setStatusWindow(this._compareWindow);
    //this._itemWindow.setStatusWindow(this._compareWindow);
    this.addWindow(this._compareWindow);
    this._lowerRightWindows.push(this._compareWindow);
    /*
    if (Imported.YEP_ItemCore && eval(Yanfly.Param.ItemSceneItem)) {
      this.createItemInfoWindow();
    }
    */
};


SWDA_EquipScene.Scene_Equip_onSlotOk = Scene_Equip.prototype.onSlotOk;
Scene_Equip.prototype.onSlotOk = function() {
    SWDA_EquipScene.Scene_Equip_onSlotOk.call(this);
    this._statusWindow._portraitSprite.hide();
    this._itemWindow.showView();
    this._commandWindow.deactivate();
    this._actorPageWindow.deactivate();
    this._actorPageWindow.hide();
};
/*
SWDA_EquipScene.Scene_Equip_onSlotCancel = Scene_Equip.prototype.onSlotCancel;
Scene_Equip.prototype.onSlotCancel = function() {
    SWDA_EquipSceneScene_Equip_onSlotCancel.call(this);
    this._statusWindow._portraitSprite.show();
};
*/

SWDA_EquipScene.Scene_Equip_onItemOk = Scene_Equip.prototype.onItemOk;
Scene_Equip.prototype.onItemOk = function() {
    // 无法卸下壶中仙头！
    if (this.actor().equips()[this._slotWindow.index()] == $dataArmors[29]){
        SoundManager.playBuzzer();
        this._itemWindow.activate()
        this._helpWindow.setText("壶中仙：干什么？给我安回去！哇呀呀呀呀呀！");
        return;
    }
    SWDA_EquipScene.Scene_Equip_onItemOk.call(this);
    this._statusWindow._portraitSprite.show();
    this._itemWindow.hideView();
    this._commandWindow.activate();
    this._slotWindow.activate();
    this._actorPageWindow.activate();
    this._actorPageWindow.show();
};

SWDA_EquipScene.Scene_Equip_onItemCancel = Scene_Equip.prototype.onItemCancel;
Scene_Equip.prototype.onItemCancel = function() {
    SWDA_EquipScene.Scene_Equip_onItemCancel.call(this);
    this._statusWindow._portraitSprite.show();
    this._itemWindow.hideView();
    this._commandWindow.activate();
    this._slotWindow.activate();
    this._actorPageWindow.activate();
    this._actorPageWindow.show();
};


SWDA_EquipScene.Scene_Equip_refreshActor = Scene_Equip.prototype.refreshActor;
Scene_Equip.prototype.refreshActor = function() {
    SWDA_EquipScene.Scene_Equip_refreshActor.call(this);
    this._slotWindow.activate();
    this._slotWindow.select(0);
};

SWDA_EquipScene.Scene_Equip_onActorChange = Scene_Equip.prototype.onActorChange;
Scene_Equip.prototype.onActorChange = function() {
    SWDA_EquipScene.Scene_Equip_onActorChange.call(this);
    this._slotWindow.activate();
    this._slotWindow.select(0);
};


Scene_Equip.prototype.updateItemPreviewWindow = function (id) {
    if (!this._itemWindow) return;
    this._itemWindow.showView()
}

Scene_Equip.prototype.hideItemPreviewBox = function () {
    if (!this._itemWindow) return;
    this._itemWindow.hideView()
}